package com.wanghua.mall.product.service.impl;

import com.wanghua.mall.product.service.BrandCategoryRelationService;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.Query;

import com.wanghua.mall.product.dao.BrandDao;
import com.wanghua.mall.product.entity.BrandEntity;
import com.wanghua.mall.product.service.BrandService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Resource
    private BrandCategoryRelationService brandCategoryRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(key)) {
            queryWrapper.like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 修改，更新品牌分类关系表
     */
    @Override
    public void updateDetail(BrandEntity brand) {
        //修改基本信息
        baseMapper.updateById(brand);
        //更新品牌分类关系表
        String name = brand.getName();
        if (StringUtils.hasLength(name)) {
            brandCategoryRelationService.updateBrand(brand.getId(), name);
        }
    }

}